var api = require('../../utils/api.js');

var app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        q_id:'',
        ones:'true',
        merch:'',
        merch_tit:'',
        merch_id:'',
        types:[],
        type:[],
        moneys:[],
        money:[],
        inuser:[],
        all:[],
        strtime:'',
        strtimeb:'00:00',
        endtime:'',
        endtimeb:'00:00',
        desc:'',
        imgs:'',
        address:'',
        xiangmu:''
    },

    getstartb(ret){
        console.log(ret);
        this.setData({
            strtimeb:ret.detail.value
        })
    },

    getendb(ret){
        this.setData({
            endtimeb:ret.detail.value
        })
    },

    getaddressa(ret){
        console.log(ret);

        var int=ret.detail.value;
        var types=this.data.addlogs;
        var address=types[int].address;
   
            console.log(types);

        this.setData({
            address:address
        })

    },

    //报价
    getmoneys(ret){
        var index=ret.detail.value;
        var moneys=this.data.moneys;
        var money=this.data.money;
        money.push(moneys[index].title);
        this.setData({
            money:money
        })
    },
    //清除报价
    clearmoneys(ret){
        console.log(ret);
        var ind=ret.currentTarget.dataset.i;
        var type=this.data.money;
        var newtype=[];
        type.forEach(function(index,item){
            if(ind!=item){
                newtype.push(index)
            }
        })
        console.log(newtype);
        this.setData({
            money:newtype
        })
    },

    //品牌
    getmerch(ret){
        var index=ret.detail.value;
        var merch=this.data.merch;
        this.setData({
            merch_tit:merch[index].title,
            merch_id:merch[index].id,
        })
    },

    // 类型
    gettypes(ret){
        var index=ret.detail.value;
        var types=this.data.types;
        var type=this.data.type;
        type.push(types[index].title);
        this.setData({
            type:type
        })
    },
    //清除类型
    cleartypes(ret){
        console.log(ret);
        var ind=ret.currentTarget.dataset.i;
        var type=this.data.type;
        var newtype=[];
        type.forEach(function(index,item){
            if(ind!=item){
                newtype.push(index)
            }
        })
        console.log(newtype);
        this.setData({
            type:newtype
        })

    },

    onLoad(ret){
        console.log(ret);
        var that=this;
        that.setData({
            q_id:ret.id,
            merch_tit:ret.title,
            merch_id:ret.merch_id
        });
    },
    
    onShow() {
        var that = this;
        this.setData({
            xiangmu: getApp().globalData.xx
        })


        let user = wx.getStorageSync('user');
        if (!user) {
            wx.navigateTo({
                url: '/pages/login/login'
            });
        }
        api.post({
            url: 'user/index',
            data: {},
            success: data => {
                var info = data.data;
                if(info.employees<1){
                    wx.showModal({
                        title: '提示',
                        content: '您没有权限使用',
                        showCancel:false,
                        success (res) {
                          wx.switchTab({
                            url: '/pages/index/index'
                          }) 
                        }
                      })
                     return false;
                }
            }
        })


        var ones=this.data.ones;

        if(ones){
          var all=wx.getStorageSync('all');
          if(all.length>0){
              that.setData({
                  all:all
              })
          }
        }

        api.post({
            url: 'index/gettime',
            data: {},
            success: data => {
                var info = data.data;
                that.setData({
                    titles: info

                })
            }
        })

    


        api.post({
            url: 'user/getaddresslog',
            data: {},
            success: data => {
                var info = data.data;
                that.setData({
                    addlogs:info
                })
            }
        })


        api.post({
            url: 'index/getallmerch',
            data: {},
            success: data => {
                var info = data.data;
                that.setData({
                    types:info.types
                })
            }
        })


        

      
    },
    
    getstart(ret){
        console.log(ret);
        var dates=ret.detail.value;
        var words = dates.split("-");
            console.log(words);
            var ye=words[0];

            console.log(ye);
            ye= ye.substring(2, 4)
            console.log(ye);

        this.setData({
            titles:ye+words[1]+words[2],
            strtime:ret.detail.value
        })
    },
    getend(ret){
        console.log(ret);
        this.setData({
            endtime:ret.detail.value
        })
    },
    getaddress(ret){
        this.setData({
            address:ret.detail.value
        })
    },
    getdesc(ret){
        this.setData({
            desc:ret.detail.value
        })
    },
    getimgs(){
        let that = this;
        var usrl=api.HOST;
        this.setData({
          ones:false
        })
        wx.chooseImage({
          count: 1,
          sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
          sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
          success: function (res) {
            console.log(res)
      
            // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
            let tempFilePaths = res.tempFilePaths;
            wx.uploadFile({
              filePath: tempFilePaths[0],
              name: 'file',
              url: usrl+'/api.php?s=portal/index/one',
              formData:{},
              success:function(res){
                var data= JSON.parse(res.data);
                that.setData({
                  imgs:data.data.img
                });
        
              },
          
            })
          }
        })
    },
    getlists(){
        wx.navigateTo({
          url: '/pages/project/list',
        })
    },


    submits(){
        var datas={};
        datas.merch=this.data.merch_id;
        datas.startime=this.data.strtime;
        datas.endtime=this.data.endtime;
        datas.address=this.data.address;

        datas.strtimeb=this.data.strtimeb;
        datas.endtimeb=this.data.endtimeb;
        datas.titles=this.data.titles;
        datas.xiangmu=this.data.xiangmu;

        datas.imgs=this.data.imgs;
        datas.desc=this.data.desc;
        datas.q_id=this.data.q_id;

        var types=this.data.type;
        var moneys=this.data.money;
        var alls=wx.getStorageSync('all');

        console.log('选择的用户',alls);
        var that=this;
        var type='';
        types.forEach(function(item){
            console.log(item);
            type=type+item+'_'
        })
        datas.types=type;

        var money='';
        moneys.forEach(function(item){
            // console.log(item);
            money=money+item+','
        })
        datas.money=money;
    
        var uids='';
        alls.forEach(function(item){
            console.log(item);
            uids=uids+item.id+','
        })
        datas.uids=uids;
        
        api.post({
          url: 'cases/addcases',
          data: datas,
          success: data => {
              console.log(data);
              wx.showToast({
                title: data.msg,
                icon:'none'
              })

              if(data.code==1){
                wx.setStorageSync('all', '');

                that.setData({
                    merch:'',
                    merch_tit:'',
                    merch_id:'',
                    types:[],
                    type:[],
                    moneys:[],
                    money:[],
                    inuser:[],
                    all:[],
                    strtime:'',
                    strtimeb:'00:00',
                    endtime:'',
                    endtimeb:'00:00',
                    desc:'',
                    imgs:'',
                    address:''
                })

                api.post({
                    url: 'cases/sendmessagebygzh',
                    data: {id:data.data.id},
                    success: data => {}
                })

                wx.reLaunch({
                  url: '/pages/project/info?id='+data.data.id
                })
              }
          }
      })
      
     
    },


    cleanint(ret){
        console.log(ret);
        var i=ret.currentTarget.dataset.i;
        var list=this.data.all;
        var that=this;
        var array=[];
        list.forEach(function(item,index){
            if(index!=i){
                array.push(item);
            }
        })

        console.log(array);

        wx.setStorageSync('all', array)
        this.setData({
            all:array
        })

    },
    xmxz(){
        wx.navigateTo({
          url: '/pages/project/xm',
        })
    }


})